package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.core.util.PropertyUtils;
import com.eling.elcms.travel.dao.IProductDao;
import com.eling.elcms.travel.model.Product;
import com.eling.elcms.travel.model.TravelContract;
import com.eling.elcms.travel.service.IProductManager;
import com.eling.elcms.travel.service.ITravelContractManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductManagerImpl extends GenericManagerImpl<Product, Long> implements IProductManager{

	IProductDao productDao;
	
	@Autowired
	private ITravelContractManager tcManager;
	
	@Autowired
	public void setDao(IProductDao dao) {
        this.dao = dao;
        this.productDao = dao;
    }
	@Override
	public List<Product> queryProdByMail(Product cond) {
		List<TravelContract> tcList = tcManager.getAll();
		List<Product> pdList = new ArrayList<Product>();
		if(!tcList.isEmpty()){
			List<Long> pkPDList = tcList.stream().map(a->a.getProductRule().getProduct().getPkProduct()).collect(Collectors.toList());
			PropertyUtils.setProperty(cond, "pkProductIn", pkPDList);
			pdList = query(cond);
		}
		return pdList;
	}
	@Override
	public Product savePro(Product product) {
		List<Product> proList = getAll();
		for(Product pro : proList){
			if(pro.getName().equals(product.getName()) && !pro.getPkProduct().equals(product.getPkProduct())){
				throw new BusinessException("名称不能重复");
			}
		}
		return save(product);
	}
	
}
